<template>
  <div></div>
</template>

<script setup lang="ts">
import { useWebApp } from 'vue-tg'

// index在这里只是负责获取信息然后存入app
const router = useRouter()
const store = useAppStore()

onMounted(async () => {
  const webapp = useWebApp()
  const startParam = webapp.initDataUnsafe.start_param || ''
  // const startParam = '1a0d080e0edb4099c29d711d1881c9f8fb805e0e35c32bdc979b34d53ac70e01'
  let tgData = null
  if (import.meta.env.MODE === 'development') {
    tgData = {
      user: {
        id: 6914547378,
        first_name: 'chen'
      }
    }
  } else {
    tgData = webapp.initDataUnsafe
    console.log('hello tg user', tgData)
  }

  if (tgData && tgData.user) {
    const tg = tgData.user
    const tgUser = { id: tg.id, first_name: tg.first_name, avatar: tg.photo_url }
    const action = await apis.api.loginActionUser({
      openid: tgUser.id,
      firstName: tgUser.first_name,
      action: startParam,
      avatar: tgUser.avatar
    })
    if (action.code > 0 && action.code < 10 && action.msg && action.msg.length > 0) {
      showDialog({ message: action.msg })
    }
    cache.setTgUser(tgUser)
    store.tgUser = tgUser
    if (action.code == 10) {
      router.replace('/redbag?id=' + action.msg)
    } else if (action.code == 11) {
      router.replace('/prize?id=' + action.msg)
    } else {
      router.replace('/home')
    }
  } else {
    router.replace('/error')
  }
})
</script>
